<?php
/**
 * 退宿计划任务
 * User: allen_ios
 * Date: 16/11/24
 * Time: 上午11:53
 */

namespace Task\App\Doorbjhq;
use Task\Core\Base;
use Task\Core\RedisQueueService;


class SyncDoorbjhqUserOut extends Base
{
    protected $mysql;

    public function __construct()
    {
        parent::__construct();
        $this->mysql = $this->selectDB('mysql');
    }


    public function getUserOutList($account_id){
        set_time_limit(0);
        $time = strtotime(date('Y-m-d'));

        $sql = "select user_id,user_name,number from gc_apartment_info where type = 3 and account_id = ".$account_id." and (cancel_time > $time)";
        $result = $this->mysql->query($sql)->fetchAll();
//        $doorctrlBjhqApi =new Service('Doorctrlbjhq/DoorctrlBjhqApi');
        $doorctrlBjhqApi = new DoorctrlBjhqApi();

        $functionName = 'cancelCard';
        foreach($result as $k=>$v) {

            $params = array(
                'userCardCode' => array(
                    'outUserId' => $v['user_id']
                )
            );

            $aa = $doorctrlBjhqApi :: callApi($functionName, $params);
//            cdump(json_encode($_SERVER));
            wLog('doorbjhq:'.var_export($aa,true).'--学号:'.$v['number'].'---姓名:'.$v['user_name'].'/n', 'doorbjhq-getUserOutList');

            //查询此人未注销的卡的信息
            $sql = "select count(*) as count from gc_door_card where user_id = ".$v['user_id']." and status = 1 and cut_off = 0";

            $isExist = $this->mysql->query($sql)->fetch(\PDO::FETCH_ASSOC);

            //如果存在此人未注销的卡，就注销掉
            if ($isExist['count'] != 0) {
                //注销此用户的卡信息
                $openCardSql = "update gc_door_card set status = 2,update_time = ".time()." where user_id = ".$v['user_id']." and status = 1 and cut_off = 0";
                $this->mysql->query($openCardSql);
            }
        }
    }

}